package com.rc.evcharger.common.utils;

import com.rc.evcharger.common.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Microcat
 * @version 1.0
 */
public class StringUtil {

    public static String getGetterMethodName(String fieldName) {
        String getter = CommonConstant.CONSTANT_STRING_GET + toUpperCaseFirstLetter(fieldName);
        return getter;
    }

    public static String getSetterMethodName(String fieldName) {
        String setter = CommonConstant.CONSTANT_STRING_SET + toUpperCaseFirstLetter(fieldName);
        return setter;
    }

    public static String toUpperCaseFirstLetter(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String toLowerCaseFirstLetter(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }


    /**
     * 解析充电的报文报文
     *
     * @param value
     * @return
     */
    public static String resolveVin(String value) {
        StringBuffer sb = new StringBuffer();
        String[] strs = value.split(" ");
        for (int i = 0; i < strs.length; i++) {
            /**过滤00，FF无效的报文，全部拼写为Z**/
            if (strs[i].equals("00") || strs[i].equals("FF")) {
                strs[i] = "5A";
            }
            sb.append(StringToHexUtil.convertHexToString(strs[i]));
        }
        return sb.toString();
    }


    /**
     * ASCII码解析充电的报文报文
     *
     * @param value
     * @return
     */
    public static String resolveShTerminal(String value) {
        StringBuffer sb = new StringBuffer();
        String[] strs = value.split(" ");
        for (int i = 0; i < strs.length; i++) {
            sb.append(StringToHexUtil.convertHexToString(strs[i]));
        }
        //去掉末尾的0
        return sb.toString().substring(0,30).trim();
    }


    /**
     * 字符串转换为Ascii
     *
     * @param value
     * @return
     */
    public static String stringToAscii(String value) {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]).append(" ");
            } else {
                sbu.append((int) chars[i]);
            }
        }
        return sbu.toString();
    }


    /**
     * 获取瑞晨30位订单号;
     *
     * @param value
     * @return
     */
    public static String getOrderNum(String value) {
        StringBuilder sb = new StringBuilder();
        String[] valueAry = value.split(" ");
        for (int i = 0; i < valueAry.length; i++) {
            //if (i == 14) {
            //    break;
            //}
            sb.append(valueAry[i]);
        }
        return sb.toString();
    }

    /**
     * 获取瑞晨30位订单号;
     *
     * @param value
     * @return
     */
    public static String getZcOrderNum(String value) {
        StringBuilder sb = new StringBuilder();
        String[] valueAry = value.split(" ");
        for (int i = 0; i < valueAry.length; i++) {
            if (i == 15) {
                break;
            }
            sb.append(valueAry[i]);
        }
        return sb.toString();
    }

    /**
     * 集合字符串转数组
     *
     * @param str
     * @return
     */
    public static String[] stringToArray(String strColl) {
        String target = StringUtils.strip(strColl, "[]");
        if (target != null) {
            return target.split(",");
        }
        return null;
    }


    /**
     * 隐藏手机号码
     *
     * @param mobile
     * @return
     */
    public static String encodeMobile(String mobile) {
        if (mobile == null || mobile.length() < 7) {
            return "该手机错误";
        }
        return mobile.substring(0, 3) + "****" + mobile.substring(7);
    }

    /**
     * 12位ruta截取并反转得到8位瑞晨ruta
     *
     * @param result
     * @return
     */
    public static String jzTurnRtua(String rtua) {
        String jzEpno = rtua.substring(6).replace(" ", "");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(jzEpno.substring(0, 2)).append(" ").append(jzEpno.substring(2, 4)).append(" ").append(jzEpno.substring(6, 8))
                .append(" ") // 后两位要反转
                .append(jzEpno.substring(4, 6));
        return stringBuffer.toString();
    }

    /**
     * 12位ruta截取并反转得到8位瑞晨ruta
     *
     * @param result
     * @return
     */
    public static String jzTurnRtua(String rtua, String sendFlag) {
        String jzEpno = rtua.replace(" ", "");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(jzEpno.substring(0, 2)).append(" ").append(jzEpno.substring(2, 4)).append(" ").append(jzEpno.substring(6, 8))
                .append(" ") // 后两位要反转
                .append(jzEpno.substring(4, 6));
        return stringBuffer.toString();
    }


    public static void main(String[] args) {
        //System.out.println(StringUtil.jzTurnRtua("19 00 14 01",null));
        System.out.println(StringUtil.resolveShTerminal("31 31 35 33 34 34 30 30 30 31 31 31 30 30 32 30 30 37 32 33 31 35 32 34 32 30 30 33 32 30 30 30"));
        System.out.println(StringUtil.stringToAscii("115344000111"));
        System.out.println(StringUtil.resolveVin("4C 5A 59 54 42 54 43 57 38 4C 31 30 31 37 37 37 31"));
    }
}
